// pages/login/phone/phone.js
const $api = require('../../../utils/api.js').API;
function gup(name, url) {
  if (!url) url = location.href;
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(url);
  return results == null ? null : results[1];
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile: '',
    code: '',
    sendCode: '获取验证码',
    codeState: true,
    uimg1: false
  },
  onLoad: function (options) {
    console.log(options)
    let _this = this
    _this.setData({
      zbType: options.zbType ? options.zbType : ''
    })
    if (options.q) {
      let queryAll = decodeURIComponent(options.q);
      var user_name = gup("user_name", queryAll)
      console.log(queryAll)
      _this.setData({ user_name: user_name, type: 1 })
    } else {
      _this.setData({ user_name: options.user_name, type: options.type })
    }

    $api.getloginconfig({
    }).then((res) => {
      _this.setData({
        Typephone: res.data.data.phone,
        Typepwd: res.data.data.pwd,
      })
    })
    //获取openid
    if (!(wx.getStorageSync('u'))) {
      wx.login({
        success(res) {
          $api.getOpenid({
            js_code: res.code
          }).then((res) => {
            wx.setStorageSync('u', res.data.data.openid)

          })
        }
      })
    }

  },
  //获取手机号
  onMobile(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  // 邀请码
  onusername(e) {
    this.setData({
      user_name: e.detail.value
    })
  },
  //获取code
  onCode(e) {
    this.setData({
      code: e.detail.value
    })
  },
  onCodeLogin(e) {
    this.setData({
      type: e.currentTarget.dataset.type
    })
  },
  //发送验证码
  onSendCode() {
    var _this = this;
    var codeState = _this.data.codeState;
    var mobile = _this.data.mobile; //手机号
    var reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/; //11位手机号码正则
    var codeTimeNum = 60;
    if (codeState) {
      if (mobile == '') {
        wx.showToast({
          title: '请输入手机号',
          icon: 'none',
          duration: 2000
        })
      } else if (!reg_tel.test(mobile)) {
        wx.showToast({
          title: '请输入正确的手机号',
          icon: 'none',
          duration: 2000
        })
      } else {
        _this.setData({
          loadModal: true
        })
        _this.setData({
          code_type: ''
        })
        $api.sendSmsCode({
          mobile: mobile,
          code_type: _this.data.type == 1 ? 2 : 1
        }).then((res) => {
          // _this.setData({
          //   code_type: res.data.data.code_type
          // })
          var codeTime = setInterval(function () {
            codeTimeNum--;
            if (codeTimeNum <= 0) {
              clearInterval(codeTime)
              _this.setData({
                codeState: true,
                sendCode: '重新获取'
              })
            } else {
              _this.setData({
                codeState: false,
                loadModal: false,
                sendCode: codeTimeNum + 's后重新获取'
              })
            }
          }, 1000)
        }).catch((err) => [
          _this.setData({
            loadModal: false
          })
        ])
      }
    }
  },
  //手机号登录
  onLogin() {
    var _this = this;
    var mobile = _this.data.mobile;
    var code = _this.data.code;
    var reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/; //11位手机号码正则
    if (mobile == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000
      })
    } else if (!reg_tel.test(mobile)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 2000
      })
    } else if (code == '') {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none',
        duration: 2000
      })
    } else {

      let type = _this.data.type
      if (type != 1) {
        if (!_this.data.uimg1) {
          wx.showToast({
            title: '请勾选协议',
            icon: 'none',
            duration: 2000
          })
          return
        }
        _this.setData({
          loadModal: true
        })
        $api.phoneLogin({
          mobile: mobile,
          code: code,
          openid: wx.getStorageSync('u'),
          // invite_user_name: _this.data.invite_user_name 
        }).then((res) => {
          _this.setData({
            loadModal: false
          })
          wx.setStorageSync('login_token', res.data.login_token);
          wx.setStorageSync('is_admin', res.data.user_info.is_admin);
          wx.setStorageSync('avatar', res.data.user_info.avatar);
          wx.setStorageSync('nick_name', res.data.user_info.nick_name);
          wx.setStorageSync('is_prod_user', res.data.user_info.is_prod_user);
          wx.setStorageSync('sceneShow', true)
          if(_this.data.zbType == 1){
            wx.redirectTo({
              url: '/pages/live/live',
            })
          }else{
            wx.redirectTo({
              url: '/pages/home/home',
            })
          }
          

        }).catch((err) => {
          _this.setData({
            loadModal: false
          })
        })
      } else {
        if (!_this.data.user_name) {
          wx.showToast({
            title: '请输入邀请码',
            icon: 'none',
            duration: 2000
          })
          return
        }
        _this.setData({
          loadModal: true
        })
        $api.registeruser({
          mobile: mobile,
          code: code,
          openid: wx.getStorageSync('u'),
          invite_user_name: _this.data.user_name ? _this.data.user_name : ''
        }).then((res) => {
          _this.setData({
            loadModal: false
          })
          wx.setStorageSync('login_token', res.data.login_token);
          wx.setStorageSync('is_admin', res.data.user_info.is_admin);
          wx.setStorageSync('avatar', res.data.user_info.avatar);
          wx.setStorageSync('nick_name', res.data.user_info.nick_name);
          wx.setStorageSync('is_prod_user', res.data.user_info.is_prod_user);
          wx.setStorageSync('user_name', res.data.user_info.user_name);
          wx.setStorageSync('sceneShow', true)
          if(_this.data.zbType == 1){
            wx.redirectTo({
              url: '/pages/live/live',
            })
          } else if (res.data.user_info.is_admin == 1 || res.data.user_info.is_admin == 2) {
            wx.redirectTo({
              url: '/pages/index/index',
            })
          } else {
            wx.redirectTo({
              url: '/pages/home/home',
            })
          }

        }).catch((err) => {
          _this.setData({
            loadModal: false
          })
        })
      }


    }
  },
  //密码登录
  onPassLogin() {
    let _this = this
    wx.redirectTo({
      url: '/pages/login/pass/pass?zbType=' + _this.data.zbType,
    })

  },
  //进入协议
  onAgreement(e) {
    wx.navigateTo({
      url: '/pages/agreement/agreement?id=' + e.currentTarget.dataset.id,
    })
  },
  onAgreementIcon(e) {
    this.setData({ uimg1: this.data.uimg1 ? false : true })
  }
})